#
# @lc app=leetcode.cn id=150 lang=python3
#
# [150] 逆波兰表达式求值
#

# @lc code=start


class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        operand_stack = []
        op_to_binary_fn = {
            "+": add,
            "-": sub,
            "*": mul,
            "/": lambda x, y: int(x / y)
        }
        for token in tokens:
            try:
                num = int(token)
            except ValueError:
                operand2 = operand_stack.pop()
                operand1 = operand_stack.pop()
                # num = self.doMatch(token, operand1, operand2)
                num = op_to_binary_fn[token](operand1, operand2)
            finally:
                operand_stack.append(num)
        
        return operand_stack.pop()

        
    def doMatch(self, op, op1, op2):
        if op == "*":
            return op1 * op2
        elif op == "/":
            return int(op1/op2)
        elif op == "+":
            return op1 + op2
        else:
            return op1 - op2
# @lc code=end

